#include "libgui.h"
#include "widget.h"

extern int16_t gx,gy;


void cb_window(struct window_t *w,uint8_t ev,int16_t x,int16_t y)
{
    for(int i=0;i<8;i++){
        if(w->list[i] == NULL)break;
        struct genery_param_t *p = w->list[i];     
        if(ev == TS_EV_RELEASE){
            p->mirror_x = p->mirror_y = 0;
        }
        if(ev == TS_EV_LEFT || ev == TS_EV_RIGHT){
            if(p->mirror_x == 0 && p->mirror_y == 0){
               p->mirror_x = p->x;
               p->mirror_y = p->y;
            }
            p->x = p->mirror_x + (x - gx);
        }
        if(ev == TS_EV_UP || ev == TS_EV_DOWN){
            if(p->mirror_x == 0 && p->mirror_y == 0){
                p->mirror_x = p->x;
                p->mirror_y = p->y;
            }
            p->y = p->mirror_y + (y - gy);
        }
				
				if(p->type == WIDGET_TYPE_MOUSE){
					p->x = x;
					p->y = y;
				}
    }
		
		

}




void cb_window_mouse(struct window_t *w,uint8_t ev,int16_t x,int16_t y)
{
    for(int i=0;i<8;i++){
        if(w->list[i] == NULL)break;
        struct genery_param_t *p = w->list[i];  
				if(p->type == WIDGET_TYPE_MOUSE){
					p->x = x;
					p->y = y;
				}
		}
}




void cb_change_color(struct genery_param_t *self,enum GUI_EVENT event)
{
    if(event & GUI_EV_LONG_PRESS){
        //self->fc = rng(0xFFFF);
    }
}




extern float back ;
extern float draw ;

void cb_change_value(struct genery_param_t *self,enum GUI_EVENT event)
{
    if(self->type == WIDGET_TYPE_BAR){
        extern void  LCD_PwmSetPulse (uint8_t pulse);
        //LCD_PwmSetPulse(self->value);
        printf("%s = %d\n",self->name,self->value);
    }
    
    if(self->type == WIDGET_TYPE_METER){
        self->value += 15;
        //printf("%s = %d\n",self->name,self->value);
    }
    
}

char static_text_buffer[1024];
void cb_change_text(struct genery_param_t *self,enum GUI_EVENT event)
{
		if(event != TS_EV_RELEASE)return;
	
		if(self->type == WIDGET_TYPE_BUTTON)
		{
			if(strstr(self->text,"Clear")){
				memset(static_text_buffer,0,1024);
				return;
			}
		}
    if(self->type == WIDGET_TYPE_BUTTON ){
		
			char *str[] = {"000000\n","11111111\n","abcdefghijklmn\n","opqrstuvwxyz\n", \
			"0123456789\n","!@#$%^&*()_+\n","test string\n","QWER\n","ASDF\n","ZXCV\n"};
			if(strlen(static_text_buffer) +32 >1024){
				printf("buffer full\n");
			}
			strcat(static_text_buffer,str[rand()%10]);
			//self->scroll = 0;
		}

}
		



struct window_t desk = {
    .backcolor = BLACK,
    .name = "main",
    .cb = cb_window,

};
struct window_t bar  = {
    .backcolor = 0x0000,
    .move = 0,
    .name = "bar",
    .cb = cb_window_mouse,
};
struct window_t key  = {
    .backcolor = 0x0000,
    .move = 0,
    .name = "key",
    .cb = cb_window_mouse,
};
struct window_t txt  = {
    .backcolor = 0x0000,
    .move = 0,
    .name = "txt",
    .cb = cb_window_mouse,
};
struct window_t ts   = {
    .backcolor = 0x0000,
    .move = 0,
    .name = "ts",
    .cb = cb_window_mouse,
};



struct genery_param_t bt01   ={
    .type = WIDGET_TYPE_BUTTON,
    .x = 390,
    .y = 230,
    .w = 80,
    .h = 40,
    .min=0,
    .max=100,
    .bc = GREY,
    .fc = RED,
    .frame_size = 4,
    .frame_color = WHITE,
    .text = "Back",
    .color_on = GREEN,
    .color_off = WHITE,
    .page = &desk,
};

struct genery_param_t bt02   ={
    .type = WIDGET_TYPE_BUTTON,
    .x = 315,
    .y = 10,
    .w = 55,
    .h = 100,
    .min=0,
    .max=100,
    .bc = GREY,
    .fc = RED,
    .frame_size = 4,
    .frame_color = WHITE,
    .color_on = GREEN,
    .color_off = GREY,
    .text = "Rand",
	.cb_event = cb_change_text,
};

struct genery_param_t bt03   ={
    .type = WIDGET_TYPE_BUTTON,
    .x = 315,
    .y = 10+105,
    .w = 55,
    .h = 145,
    .min=0,
    .max=100,
    .bc = GREY,
    .fc = RED,
    .frame_size = 4,
    .frame_color = WHITE,
    .color_on = GREEN,
    .color_off = GREY,
    .text = "Clear",
	.cb_event = cb_change_text,
};


struct genery_param_t sw01   ={
    .type = WIDGET_TYPE_SW,
    .x = 50,
    .y = 80,
    .w = 40,
    .h = 100,
    .bc = WHITE,
    .frame_size = 4,
    .color_on = GREEN,
    .color_off = GREY,
    .frame_color = GREY,
    .text = "sw01",
    .name = "sw01",
    .value = 0,
    .cb_event = cb_change_value,

};

struct genery_param_t sw02   ={
    .type = WIDGET_TYPE_SW,
    .x = 150,
    .y = 100,
    .w = 150,
    .h = 60,
    .bc = WHITE,
    .frame_size = 4,
    .color_on = GREEN,
    .color_off = GREY,
    .frame_color = GREY,
    .text = "sw02",
    .name = "sw02",
    .value = 1,
    .cb_event = cb_change_value,

};

struct genery_param_t sw03   ={
    .type = WIDGET_TYPE_SW,
    .x = 360,
    .y = 100,
    .w = 60,
    .h = 60,
    .bc = WHITE,
    .frame_size = 4,
    .color_on = GREEN,
    .color_off = GREY,
    .frame_color = GREY,
    .text = "sw03",
    .name = "sw03",
    .value = 0,
    .cb_event = cb_change_value,

};

struct genery_param_t bar01 = {
    .type = WIDGET_TYPE_BAR,
    .w = 340,
    .h = 40,
    .bc = WHITE,
    .fc = 0xff,
    .frame_size = 4,
    .frame_color = GREY,
    .color = GREEN,
    .x = 100,
    .y = 50,
    .min = 0,
    .max = 100,
    .value = 50,
    .name = "bar01",
    .cb_event = cb_change_value,

};
struct genery_param_t bar02 = {
    .type = WIDGET_TYPE_BAR,
    .w = 340,
    .h = 40,
    .bc = WHITE,
    .fc = 0xff,
    .frame_size = 4,
    .frame_color = GREY,
    .color = GREEN,
    .x = 100,
    .y = 120,
    .min = 0,
    .max = 100,
    .value = 50,
    .name = "bar02",
    .cb_event = cb_change_value,

};

struct genery_param_t bar03 = {
    .type = WIDGET_TYPE_BAR,
    .w = 40,
    .h = 220,
    .bc = WHITE,
    .fc = 0xff,
    .frame_size = 4,
    .frame_color = GREY,
    .color = CYAN,
    .x = 30,
    .y = 30,
    .min = 0,
    .max = 50,
    .value = 10,
    .name = "bar03",
    .cb_event = cb_change_value,

};
struct genery_param_t bat01 = {
    .type = WIDGET_TYPE_BAT,
    .w = 60,
    .h = 20,
    .bc = LGRAY,
    .fc = 0xffff,
    .frame_size = 2,
    .frame_color = DGRAY,
    .x = 60,
    .y = 60,
    .value = 50,
};

struct genery_param_t txt01 = {
    .type = WIDGET_TYPE_TEXTBOX,
    .w = 300,
    .h = 250,
    .bc = WHITE,
    .fc = RED,
    .frame_size = 2,
    .frame_color = GREY,
    .x = 10,
    .y = 10,
	.text = static_text_buffer,
	.max = 1024,
    //.text = "123-----AAAA\nhalo\n000\n111\n222\n333\n444\n555\n666\n777\n888\0",
    .font = NULL,
    .scroll = 0,
};


struct genery_param_t icon00 = {
    .type = WIDGET_TYPE_ICON,
    .w = 120,
    .h = 120,
    .bc = WHITE,
    .fc = RED,
    .frame_size = 2,
    .frame_color = GREEN,
    .x = 0,
    .y = 60,
    .mirror_x = 0,
    .mirror_y = 0,
    .font = NULL,
    .text = "0",
    .page = &bar,
};

struct genery_param_t icon01 = {
    .type = WIDGET_TYPE_ICON,
    .w = 120,
    .h = 120,
    .bc = YELLOW,
    .fc = RED,
    .frame_size = 2,
    .frame_color = CYAN,
    .x = 0 + 120 + 30,
    .y = 60,
    .mirror_x = 0,
    .mirror_y = 0,
    .font = NULL,
    .text = "1",
    .page = &key,
};

struct genery_param_t icon02 = {
    .type = WIDGET_TYPE_ICON,
    .w = 120,
    .h = 120,
    .bc = CYAN,
    .fc = RED,
    .frame_size = 2,
    .frame_color = CYAN,
    .x = 300,
    .y = 60,
    .mirror_x = 0,
    .mirror_y = 0,
    .font = NULL,
    .text = "2",
    .page = &txt,
};

struct genery_param_t icon03 = {
    .type = WIDGET_TYPE_ICON,
    .w = 120,
    .h = 120,
    .bc = WHITE,
    .fc = RED,
    .frame_size = 2,
    .frame_color = CYAN,
    .x = 450,
    .mirror_x = 0,
    .mirror_y = 0,
    .y = 60,
    .font = NULL,
    .text = "3",
    .page = &ts,
    .cb_event = cb_change_color,
};


struct genery_param_t timer01= {
    .type = WIDGET_TYPE_TIMER,
    .w = 799,
    .h = 479,
    .bc = WHITE,
    .fc = RED,
    .frame_size = 2,
    .frame_color = CYAN,
    .x = 0,
    .y = 0,
    .font = NULL,
    .text = "3",
    .cb_event = cb_change_color,
};


struct genery_param_t meter00= {
    .type = WIDGET_TYPE_METER,
    .name = "meter",
    .value = 0,
    .cb_event = cb_change_value,
    .x=50,
    .y=70,
    .w=200,
    .h=200,
    .color = WHITE,
    .bc = DGRAY,
    .fc = RED,
    .min = 0,
    .max = 360,
};


struct genery_param_t mouse00= {
    .type = WIDGET_TYPE_MOUSE,
    .name = "mouse",
    .value = 0,
    .x=10,
    .y=10,
    .w=15,
    .h=15,
    .color = RED,
    .bc = DGRAY,
    .fc = RED,
};

void desktop_demo(void)
{
    
    window_create(&desk,0,0,WIDTH ,HIGHT);
    window_create(&bar,0,0,WIDTH,HIGHT);
    window_create(&key,0,0,WIDTH,HIGHT);
    window_create(&txt,0,0,WIDTH,HIGHT);
    window_create(&ts,0,0,WIDTH,HIGHT);

    window_add(&desk,&icon00);
    window_add(&desk,&icon01);
    window_add(&desk,&icon02);
    window_add(&desk,&icon03);
    window_add(&desk,&mouse00);
    
    window_add(&bar,&bar01);
    window_add(&bar,&bar02);
    window_add(&bar,&bar03);
    window_add(&bar,&bt01);
    
    window_add(&key,&sw01);
    window_add(&key,&sw02);
    window_add(&key,&sw03);
    window_add(&key,&bt01);
    
    window_add(&txt,&txt01);
    window_add(&txt,&bt01);
    window_add(&txt,&bt02);
    window_add(&txt,&bt03);
    
    window_add(&ts,&meter00);
    window_add(&ts,&bt01);
		
    window_add(&key,&mouse00);
    window_add(&txt,&mouse00);
    window_add(&bar,&mouse00);
    window_add(&ts,&mouse00);
		
    window_show(&desk);  
    
}


